Loop through Objects

Hi,
I am currently attempting to loop through all of the objects in a project. I have the program working fine when I'm looping through all of the objects in a module, but I am having problems when attempting to loop through all of the modules in the project. I'm thinking that I would use nested loops to get this done, but what variables should I use and how would I declare the outer loop (the loop used to loop through ALL (not just the open) modules in a project?
Thanks!
josiah820 - Fri Jun 01 09:18:58 EDT 2012

Re: Loop through Objects
SystemAdmin - Fri Jun 01 09:31:29 EDT 2012

Here is some simple DXL code that will loop through all modules in the current project.
 

Module m
Item i
string itemType
string modName, fullModName, modPrefix
 
for i in current Project do
    {
 
        itemType = type i
 
        if( itemType == "Formal")
                {
 
                fullModName = fullName(i)
                modName = name(i)
                
                m = read(fullModName,false)
                modPrefix = m."Prefix"
                print modName ": [" modPrefix "]\n"
                close(m)
                }
        }

 

 


Paul Miller
Melbourne, Australia

 

 

Re: Loop through Objects
Peter_Albert - Fri Jun 01 09:31:52 EDT 2012

You can loop over all items in a project to recursively get handles to all Modules in this project. However, the order of Modules does not follow the GUI display order. Louie has provided a nice code snippet to loop over all Modules in the order as they are displayed (and a nice side-effect that it works for folders as well). See here: http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14290040

Peter

Re: Loop through Objects
josiah820 - Fri Jun 01 13:50:19 EDT 2012

SystemAdmin - Fri Jun 01 09:31:29 EDT 2012

Here is some simple DXL code that will loop through all modules in the current project.
 

Module m
Item i
string itemType
string modName, fullModName, modPrefix
 
for i in current Project do
    {
 
        itemType = type i
 
        if( itemType == "Formal")
                {
 
                fullModName = fullName(i)
                modName = name(i)
                
                m = read(fullModName,false)
                modPrefix = m."Prefix"
                print modName ": [" modPrefix "]\n"
                close(m)
                }
        }

 

 


Paul Miller
Melbourne, Australia

 

 

Would there by any way for me to just loop through all of the objects? I'm so close to finishing this program, and that's the only thing between me and completing the program.
Thanks again!

Re: Loop through Objects
SystemAdmin - Sat Jun 02 04:45:32 EDT 2012

josiah820 - Fri Jun 01 13:50:19 EDT 2012
Would there by any way for me to just loop through all of the objects? I'm so close to finishing this program, and that's the only thing between me and completing the program.
Thanks again!

That would be a simple case of nesting an Object loop within the Item loop - see code below where I have added an Object loop that just counts the number of objects.

Some important points about the item and object loops in this code.

The Item loop will open each successive undeleted item that is a Formal type module, for which the user running this code has at least read access in the project.

The Object loop will touch each successive object that is visible to the user (Including visible Table Cell Objects), it will include deleted objects if they are enabled to be visible - you may not want this so make sure that the default opening view doesn't apply view settings that activate a filter and\or shows deleted objects. There are a number of different Object loops available, so perhaps a read of the DXL Manual is recommended - just select the Help menu in the DOORS Explorer and look for the "DXL Reference Manual", the chapter to look at is Objects > Finding Objects
 

Module m
Object o
Item i
int objCount
string itemType
string modName, fullModName, modPrefix
 
for i in current Project do //Loops through each undeleted and visible item 
    {
 
        itemType = type i
 
        if( itemType == "Formal")
                {
 
                fullModName = fullName(i)
                modName = name(i)
                
                m = read(fullModName,false)
                modPrefix = m."Prefix"
                print modName ": [" modPrefix "]\n"
                objCount=0
 
                for o in m do { //Loops through each undeleted and visible object
                        objCount++
                        }
                print "\t Number of undeleted objects: " objCount "\n\n"
 
                close(m)
                }
        }

 


Paul Miller
Melbourne, Australia

 

Re: Loop through Objects
josiah820 - Mon Jun 04 08:59:30 EDT 2012

SystemAdmin - Sat Jun 02 04:45:32 EDT 2012

That would be a simple case of nesting an Object loop within the Item loop - see code below where I have added an Object loop that just counts the number of objects.

Some important points about the item and object loops in this code.

The Item loop will open each successive undeleted item that is a Formal type module, for which the user running this code has at least read access in the project.

The Object loop will touch each successive object that is visible to the user (Including visible Table Cell Objects), it will include deleted objects if they are enabled to be visible - you may not want this so make sure that the default opening view doesn't apply view settings that activate a filter and\or shows deleted objects. There are a number of different Object loops available, so perhaps a read of the DXL Manual is recommended - just select the Help menu in the DOORS Explorer and look for the "DXL Reference Manual", the chapter to look at is Objects > Finding Objects
 

Module m
Object o
Item i
int objCount
string itemType
string modName, fullModName, modPrefix
 
for i in current Project do //Loops through each undeleted and visible item 
    {
 
        itemType = type i
 
        if( itemType == "Formal")
                {
 
                fullModName = fullName(i)
                modName = name(i)
                
                m = read(fullModName,false)
                modPrefix = m."Prefix"
                print modName ": [" modPrefix "]\n"
                objCount=0
 
                for o in m do { //Loops through each undeleted and visible object
                        objCount++
                        }
                print "\t Number of undeleted objects: " objCount "\n\n"
 
                close(m)
                }
        }

 


Paul Miller
Melbourne, Australia

 

I tried implementing this code, and it's not even getting into the "item in Project" loop. I'm putting it in exactly as you are suggesting. Do you have any idea of how to fix this problem? I can show you my code if it would help...
Thanks for all your help!

Re: Loop through Objects
SystemAdmin - Mon Jun 04 09:27:53 EDT 2012

josiah820 - Mon Jun 04 08:59:30 EDT 2012
I tried implementing this code, and it's not even getting into the "item in Project" loop. I'm putting it in exactly as you are suggesting. Do you have any idea of how to fix this problem? I can show you my code if it would help...
Thanks for all your help!

Firstly, if you copy the simple code that I posted as it appears into the DXL editor and run it, it should execute without error. If you can get this code to work as it is on your DOORS installation then all we have to do is work out what is different about how you have implemented this loop in your code.

Please post your code and lets have a look at it. If it's a large piece of code, probably easier if you post it as an attachment rather than embedding it as text.


Paul Miller
Melbourne, Australia

Re: Loop through Objects
josiah820 - Mon Jun 04 09:59:58 EDT 2012

SystemAdmin - Mon Jun 04 09:27:53 EDT 2012
Firstly, if you copy the simple code that I posted as it appears into the DXL editor and run it, it should execute without error. If you can get this code to work as it is on your DOORS installation then all we have to do is work out what is different about how you have implemented this loop in your code.

Please post your code and lets have a look at it. If it's a large piece of code, probably easier if you post it as an attachment rather than embedding it as text.


Paul Miller
Melbourne, Australia

Here's my code. The code that you suggested starts around line 394. If there are any suggestions that you have about the code as a whole, I'm open to suggestions! I've only been working with DXL for a few weeks now, so I'm really new at this!
Thanks so much for all of your help!
Attachments

attachment_14831246_exportByDate.dxl

Re: Loop through Objects
josiah820 - Mon Jun 04 13:39:51 EDT 2012

SystemAdmin - Mon Jun 04 09:27:53 EDT 2012
Firstly, if you copy the simple code that I posted as it appears into the DXL editor and run it, it should execute without error. If you can get this code to work as it is on your DOORS installation then all we have to do is work out what is different about how you have implemented this loop in your code.

Please post your code and lets have a look at it. If it's a large piece of code, probably easier if you post it as an attachment rather than embedding it as text.


Paul Miller
Melbourne, Australia

Yes, I'm aware that there are a ridiculous number of comments in the script. I am interning for a company and was asked to put a lot of comments in my code so that the people that are still here when my internship is complete will be able to make any changes needed in the future.

Re: Loop through Objects
llandale - Mon Jun 04 17:51:18 EDT 2012

josiah820 - Mon Jun 04 08:59:30 EDT 2012
I tried implementing this code, and it's not even getting into the "item in Project" loop. I'm putting it in exactly as you are suggesting. Do you have any idea of how to fix this problem? I can show you my code if it would help...
Thanks for all your help!

You need to be inside some project; blue foler opened in the left pane of the Explorer.

Re: Loop through Objects
SystemAdmin - Mon Jun 04 18:25:22 EDT 2012

josiah820 - Mon Jun 04 13:39:51 EDT 2012
Yes, I'm aware that there are a ridiculous number of comments in the script. I am interning for a company and was asked to put a lot of comments in my code so that the people that are still here when my internship is complete will be able to make any changes needed in the future.

I had a look at your code and see that Louie has responded and hit on the problem. Just to elaborate a bit here as it may not be the bleeding obvious to you and others who could fall into the same trap.

The "for item in current Project" looping snippet in your code looks as though it should work.

The reason for it not being entered is because an item such as a blue Project folder, or a yellow folder within a project, or a module within a project, has not been selected in the DOORS Explorer, thus negating the possibility of having a "current Project".

For example, if the current selection in the DOORS Explorer is the root of the database, or is a yellow standard folder created at the root (i.e. it has no parent blue Project folder), then neither of these would be picked up as being the current Project and the loop will not be entered.

Adding the line if (current Project == null) print "Null" just before the loop starts should confirm if this is the case, and if so, you need to add this as an exception test and raise a warning to the user that a folder or a module within a DOORS Project has not been selected.


Paul Miller
Melbourne, Australia

Re: Loop through Objects
SystemAdmin - Mon Jun 04 18:37:39 EDT 2012

josiah820 - Mon Jun 04 13:39:51 EDT 2012
Yes, I'm aware that there are a ridiculous number of comments in the script. I am interning for a company and was asked to put a lot of comments in my code so that the people that are still here when my internship is complete will be able to make any changes needed in the future.

Just one more point - I noticed you defined Item i as a global, be careful with a global like this one rather than defining it local to a function, or maybe passing in as an argument. Can be the source of some debugging grief when multiple functions can alter the one global variable and you get into a vicious circle of potentially invalid data being bounced between functions.


Paul Miller
Melbourne, Australia